#!/bin/bash

# 1 文本内容替换（区分大小写，使用Sublime的话，可以使用Find in Files...进行全文替换，路径中去除-package.json,-*.js,-*.bin）
function replaceText() {
	if [[ $1 = */build || $1 = */node_modules || $1 = */dist || $1 = */static || $1 = */generated ]]; then
		return
	fi

	# echo "Entering $1 ..."
	for f in $(ls "$1")
	do
		if [[ -f $1/$f ]]; then
			if [[ ! ( $f = "README.md" || $f = *.bin || $f = *.sh || $f = *.png || $f = *.gif || $f = *.svg || $f = *.jar ) ]]; then
				sed -i "" "s/$2/$3/g" "$1/$f"
			fi
		elif [[ -d $1/$f ]]; then
            if [[ $f = "moduleA" ]]; then
                mv "$1/$f" "$1/$4"
                replaceText "$1/$4" $2 $3 $4
            else
                replaceText "$1/$f" $2 $3 $4
            fi
		fi
	done
	# echo "Leaving $1 ..."
}

if [[ $# < 3 ]]; then
	service_name=$1
	Service_name=$2
	module_name=$service_name
else
	service_name=$1
	Service_name=$2
	module_name=$3
fi

# skeleton->xrshop
replaceText . "skeleton" $service_name $module_name

# Skeleton->Xrshop
replaceText . "Skeleton" $Service_name $module_name

# 2 文件夹重命名
# admin/src/main/java/com/xingren/skeleton -> admin/src/main/java/com/xingren/xrshop
mv admin/src/main/java/com/xingren/skeleton admin/src/main/java/com/xingren/$service_name

# admin/src/test/java/com/xingren/skeleton -> admin/src/test/java/com/xingren/xrshop
mv admin/src/test/java/com/xingren/skeleton admin/src/test/java/com/xingren/$service_name

# api/src/main/java/com/xingren/skeleton -> api/src/main/java/com/xingren/xrshop
mv api/src/main/java/com/xingren/skeleton api/src/main/java/com/xingren/$service_name

# api/src/test/java/com/xingren/skeleton -> api/src/test/java/com/xingren/xrshop
mv api/src/test/java/com/xingren/skeleton api/src/test/java/com/xingren/$service_name

# model/src/main/java/com/xingren/skeleton -> model/src/main/java/com/xingren/xrshop
mv model/src/main/java/com/xingren/skeleton model/src/main/java/com/xingren/$service_name

# services/src/main/java/com/xingren/skeleton -> services/src/main/java/com/xingren/xrshop
mv services/src/main/java/com/xingren/skeleton services/src/main/java/com/xingren/$service_name

# sdk/gensrc/main/java/com/xingren/service/skeleton -> sdk/gensrc/main/java/com/xingren/service/xrshop
mv sdk/gensrc/main/java/com/xingren/service/skeleton sdk/gensrc/main/java/com/xingren/service/$service_name

# sdk/src/main/java/com/xingren/service/skeleton -> sdk/src/main/java/com/xingren/service/xrshop
mv sdk/src/main/java/com/xingren/service/skeleton sdk/src/main/java/com/xingren/service/$service_name

# api/src/test/resources/db/contract/xrshop/skeleton -> api/src/test/resources/db/contract/xrshop/xrshop
mv api/src/test/resources/db/contract/$module_name/skeleton api/src/test/resources/db/contract/$module_name/$service_name

# 3 文件名替换
# services/src/main/java/com/xingren/skeleton/Skeleton*Errors.java->Xrshop*Errors.java
mv services/src/main/java/com/xingren/$service_name/SkeletonApiErrors.java "services/src/main/java/com/xingren/$service_name/${Service_name}ApiErrors.java"
mv services/src/main/java/com/xingren/$service_name/SkeletonDomainErrors.java "services/src/main/java/com/xingren/$service_name/${Service_name}DomainErrors.java"

# api/src/main/java/com/xingren/skeleton/api/moduleA/controller/SkeletonController.java->XrshopController.java
mv api/src/main/java/com/xingren/$service_name/api/$module_name/controller/SkeletonController.java "api/src/main/java/com/xingren/$service_name/api/$module_name/controller/${Service_name}Controller.java"

# sdk/gensrc/main/java/com/xingren/service/skeleton/client/api/SkeletonApi.java->XrshopApi.java
mv sdk/gensrc/main/java/com/xingren/service/$service_name/client/api/SkeletonApi.java "sdk/gensrc/main/java/com/xingren/service/$service_name/client/api/${Service_name}Api.java"

# sdk/src/main/java/com/xingren/service/skeleton/client/SkeletonClientConfig.java->XrshopClientConfig.java
mv sdk/src/main/java/com/xingren/service/$service_name/client/SkeletonClientConfig.java "sdk/src/main/java/com/xingren/service/$service_name/client/${Service_name}ClientConfig.java"

# sdk/src/main/java/com/xingren/service/skeleton/client/SkeletonConstants.java->XrshopConstants.java
mv sdk/src/main/java/com/xingren/service/$service_name/client/SkeletonConstants.java "sdk/src/main/java/com/xingren/service/$service_name/client/${Service_name}Constants.java"

# sdk/src/main/java/com/xingren/service/skeleton/client/SkeletonHystrixConfig.java->XrshopHystrixConfig.java
mv sdk/src/main/java/com/xingren/service/$service_name/client/SkeletonHystrixConfig.java "sdk/src/main/java/com/xingren/service/$service_name/client/${Service_name}HystrixConfig.java"
